import { PureComponent } from "react"
import store from "../../../store"
import { changeAgeAction } from "../../../store/actionCreators"

export default class Com2 extends PureComponent {
  constructor() {
    super()
    this.state = {
      age: store.getState().age
    }
  }
  componentDidMount() {
    store.subscribe(() => {
      console.log(store.getState().age)
      this.setState({
        // 强制触发视图更新
        age: store.getState().age
      })
    })
  }
  onSub(n) {
    store.dispatch(changeAgeAction(n))
  }
  render() {
    return (
      <div>
        <div>Com2:{this.state.age}</div>
        <div>
          <button
            onClick={() => {
              this.onSub(-1)
            }}
          >
            -1
          </button>
          <button
            onClick={() => {
              this.onSub(-2)
            }}
          >
            -2
          </button>
          <button
            onClick={() => {
              this.onSub(-5)
            }}
          >
            -5
          </button>
        </div>
      </div>
    )
  }
}
